/*
 *搜索控件（ps:目前只单条件）
 *调用方式如下
 * var page = new PageControl();
 * page.init({
 *         totalCount:15,    //总页数
 *         conter:".app",    //容器选择器
 *         nowPage:1,        //当前页，默认第几页
 *         url:"localhost:5331", //接口请求api
 *         data:{                //api传参，未接入扩展参数
 *             text:11
 *         }
 *     });
 */

var Search = function () {
    var result = {
        keyList: null,
        conter: 'body',
        parameter:null,
        callback:null,
        init: function (obj) {
            //初始化
            this.contact(obj);
            this.renderTemplate();
        },
        contact: function (obj) {
            for (var key in obj) {
                this[key] = obj[key];
            }
        },
        addEvent:function(){
            var self = this;
            $(this.conter).find('.ppm-search-btn').off('click').on('click',function(){
                var para = self.getParameter();
                if(para==null) {
                    alert("请选择要搜索的字段！");
                    return;
                }
                self.parameter = para;
                //进行搜索，调用回掉
                typeof(self.callback) == "function" && self.callback(para);
            });
            //回车事件
            $(this.conter).find('[name=ppmSearchingValue]').off('keydown').on('keydown',function(e){
                if(e.keyCode==13){
                    var para = self.getParameter();
                    if(para==null) {
                        alert("请选择要搜索的字段！");
                        return;
                    }
                    self.parameter = para;
                    //进行搜索，调用回掉
                    typeof(self.callback) == "function" && self.callback(para);
                }
            });
        },
        renderTemplate: function () {
            //模板渲染
            $(this.conter).html('');
            _html = '';
            _html += '<div class="ppm-single-search">\
          <select name="ppmSearchingKey" class="ppm-search-select" id="">\
          <option value="-1">请选择</option>';
            //添加选择字段
            for (var key in this.keyList) {
                _html += '<option value="' + this.keyList[key].enName + '">' + this.keyList[key].cnName + '</option>';
            }
            _html += '</select>\
                        <input name="ppmSearchingValue" placeholder="请输入" class="ppm-search-input" />\
                        <a class="ppm-search-btn ppm-btn ppm-btn-green">搜索</a>\
                        <div class="clear"></div>\
                    </div>';
            $(this.conter).append(_html);
            this.addEvent();
        },
        getParameter:function(){
            var result = {};
            var keys = $(this.conter).find('select').val();
            var val = $(this.conter).find('input').val();
            //如果没有选择，提示不进行搜索；
            if(keys=='-1'){return null;}
            result['key'] = keys;
            result['value'] = val;
            result[keys] = val;
            return result;
        }
    };
    return result;
};